package com.ruoyi.commentmanage.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.commentmanage.mapper.CmCommentAppendMapper;
import com.ruoyi.commentmanage.service.ICmCommentAppendService;
import com.ruoyi.commentmanage.domain.CmCommentAppend;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.commentmanage.domain.CmComment;
import com.ruoyi.useradmin.domain.UaUser;
import com.ruoyi.commentmanage.service.ICmCommentService;
import com.ruoyi.useradmin.service.IUaUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class CmCommentAppendServiceImpl implements ICmCommentAppendService {


    @Resource
    private CmCommentAppendMapper cmCommentAppendMapper;
    
    
    @Resource
    private ICmCommentService cmCommentService;
    @Resource
    private IUaUserService uaUserService;

    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param cmCommentAppend 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<CmCommentAppend> getCmCommentAppendPage(PageDomain pageDomain, CmCommentAppend cmCommentAppend , CmComment cmComment) {
        if (Objects.isNull(cmCommentAppend)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<CmCommentAppend> queryWrapper = new LambdaQueryWrapper<>(new CmCommentAppend());
            //if(!Objects.isNull(cmCommentAppend.getName())){
            //	queryWrapper.like(CmCommentAppend::getName,cmCommentAppend.getName());
            //}
            

            if (!Objects.isNull(cmCommentAppend.getId())) {
                queryWrapper.eq(CmCommentAppend::getId, cmCommentAppend.getId());
            }

            if (!Objects.isNull(cmCommentAppend.getCmCommentId())) {
                queryWrapper.eq(CmCommentAppend::getCmCommentId, cmCommentAppend.getCmCommentId());
            }

            cmComment.setBaseEntityNull();
            cmComment.setId(null);
            if (!PageDomain.checkObjAllFieldsIsNull(cmComment)) {
                List<Long> idList = cmCommentService.getIdListByCmComment(cmComment);
                queryWrapper.in(CmCommentAppend::getCmCommentId, idList);
            }


            if (!Objects.isNull(cmCommentAppend.getUaUserId())) {
                queryWrapper.eq(CmCommentAppend::getUaUserId, cmCommentAppend.getUaUserId());
            }

            if (!Objects.isNull(cmCommentAppend.getCommentText())) {
                queryWrapper.like(CmCommentAppend::getCommentText, cmCommentAppend.getCommentText());
            }

            if (!Objects.isNull(cmCommentAppend.getPraiseNum())) {
                queryWrapper.like(CmCommentAppend::getPraiseNum, cmCommentAppend.getPraiseNum());
            }

            if (!Objects.isNull(cmCommentAppend.getStatus())) {
                queryWrapper.like(CmCommentAppend::getStatus, cmCommentAppend.getStatus());
            }

            if (!Objects.isNull(cmCommentAppend.getDelFlag())) {
                queryWrapper.like(CmCommentAppend::getDelFlag, cmCommentAppend.getDelFlag());
            }

            if (!Objects.isNull(cmCommentAppend.getCreateBy())) {
                queryWrapper.like(CmCommentAppend::getCreateBy, cmCommentAppend.getCreateBy());
            }

            if (!Objects.isNull(cmCommentAppend.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(cmCommentAppend.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(cmCommentAppend.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(CmCommentAppend::getCreateTime, cmCommentAppend.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(cmCommentAppend.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(CmCommentAppend::getCreateTime, cmCommentAppend.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(cmCommentAppend.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(cmCommentAppend.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(CmCommentAppend::getCreateTime, cmCommentAppend.getParams().get("CreateTimeBeginTime"), cmCommentAppend.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(cmCommentAppend.getUpdateBy())) {
                queryWrapper.like(CmCommentAppend::getUpdateBy, cmCommentAppend.getUpdateBy());
            }

            if (!Objects.isNull(cmCommentAppend.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(cmCommentAppend.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(cmCommentAppend.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(CmCommentAppend::getUpdateTime, cmCommentAppend.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(cmCommentAppend.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(CmCommentAppend::getUpdateTime, cmCommentAppend.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(cmCommentAppend.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(cmCommentAppend.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(CmCommentAppend::getUpdateTime, cmCommentAppend.getParams().get("UpdateTimeBeginTime"), cmCommentAppend.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(cmCommentAppend.getRemark())) {
                queryWrapper.like(CmCommentAppend::getRemark, cmCommentAppend.getRemark());
            }
        Page<CmCommentAppend> cmCommentAppendList= cmCommentAppendMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(CmCommentAppend record:cmCommentAppendList.getRecords()){
            record.setCmComment(cmCommentService.getCmCommentById(record.getCmCommentId()));
            record.setUaUser(uaUserService.getUaUserById(record.getUaUserId()));
        }

            return cmCommentAppendList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public CmCommentAppend getCmCommentAppendById(Long id) {
        if (Objects.isNull(id)) {
            return new CmCommentAppend();
        }
        try {
            CmCommentAppend cmCommentAppend = cmCommentAppendMapper.selectById(id);
            cmCommentAppend.setCmComment(cmCommentService.getCmCommentById(cmCommentAppend.getCmCommentId()));
            cmCommentAppend.setUaUser(uaUserService.getUaUserById(cmCommentAppend.getUaUserId()));
            if (Objects.isNull(cmCommentAppend)){
                cmCommentAppend = new CmCommentAppend();
            }
            return cmCommentAppend;

        } catch (Exception e) {
            e.printStackTrace();
            return new CmCommentAppend();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param cmCommentAppend 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByCmCommentAppend(CmCommentAppend cmCommentAppend) {
        if (Objects.isNull(cmCommentAppend)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<CmCommentAppend> queryWrapper = new LambdaQueryWrapper<>(new CmCommentAppend());
            

            if (!Objects.isNull(cmCommentAppend.getId())) {
                queryWrapper.eq(CmCommentAppend::getId, cmCommentAppend.getId());
            }

            if (!Objects.isNull(cmCommentAppend.getCmCommentId())) {
                queryWrapper.eq(CmCommentAppend::getCmCommentId, cmCommentAppend.getCmCommentId());
            }

            if (!Objects.isNull(cmCommentAppend.getUaUserId())) {
                queryWrapper.eq(CmCommentAppend::getUaUserId, cmCommentAppend.getUaUserId());
            }

            if (!Objects.isNull(cmCommentAppend.getCommentText())) {
                queryWrapper.like(CmCommentAppend::getCommentText, cmCommentAppend.getCommentText());
            }

            if (!Objects.isNull(cmCommentAppend.getPraiseNum())) {
                queryWrapper.like(CmCommentAppend::getPraiseNum, cmCommentAppend.getPraiseNum());
            }

            if (!Objects.isNull(cmCommentAppend.getStatus())) {
                queryWrapper.like(CmCommentAppend::getStatus, cmCommentAppend.getStatus());
            }

            if (!Objects.isNull(cmCommentAppend.getDelFlag())) {
                queryWrapper.like(CmCommentAppend::getDelFlag, cmCommentAppend.getDelFlag());
            }

            if (!Objects.isNull(cmCommentAppend.getCreateBy())) {
                queryWrapper.like(CmCommentAppend::getCreateBy, cmCommentAppend.getCreateBy());
            }

            if (!Objects.isNull(cmCommentAppend.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(cmCommentAppend.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(cmCommentAppend.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(CmCommentAppend::getCreateTime, cmCommentAppend.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(cmCommentAppend.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(CmCommentAppend::getCreateTime, cmCommentAppend.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(cmCommentAppend.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(cmCommentAppend.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(CmCommentAppend::getCreateTime, cmCommentAppend.getParams().get("CreateTimeBeginTime"), cmCommentAppend.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(cmCommentAppend.getUpdateBy())) {
                queryWrapper.like(CmCommentAppend::getUpdateBy, cmCommentAppend.getUpdateBy());
            }

            if (!Objects.isNull(cmCommentAppend.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(cmCommentAppend.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(cmCommentAppend.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(CmCommentAppend::getUpdateTime, cmCommentAppend.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(cmCommentAppend.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(CmCommentAppend::getUpdateTime, cmCommentAppend.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(cmCommentAppend.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(cmCommentAppend.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(CmCommentAppend::getUpdateTime, cmCommentAppend.getParams().get("UpdateTimeBeginTime"), cmCommentAppend.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(cmCommentAppend.getRemark())) {
                queryWrapper.like(CmCommentAppend::getRemark, cmCommentAppend.getRemark());
            }

            List<CmCommentAppend> cmCommentAppends = cmCommentAppendMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (CmCommentAppend cmCommentAppendNo:cmCommentAppends){
                idList.add(cmCommentAppendNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 通过cmComment外键idList查询idList
     *
     * @param cmCommentIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByCmCommentIdList(List<Long> cmCommentIdList) {
        if (Objects.isNull(cmCommentIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<CmCommentAppend> queryWrapper = new LambdaQueryWrapper<>(new CmCommentAppend());
            queryWrapper.in(CmCommentAppend::getCmCommentId, cmCommentIdList);
            List<CmCommentAppend> cmCommentAppends = cmCommentAppendMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (CmCommentAppend cmCommentAppend:cmCommentAppends){
                idList.add(cmCommentAppend.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }


    /**
     * 通过uaUser外键idList查询idList
     *
     * @param uaUserIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByUaUserIdList(List<Long> uaUserIdList) {
        if (Objects.isNull(uaUserIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<CmCommentAppend> queryWrapper = new LambdaQueryWrapper<>(new CmCommentAppend());
            queryWrapper.in(CmCommentAppend::getUaUserId, uaUserIdList);
            List<CmCommentAppend> cmCommentAppends = cmCommentAppendMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (CmCommentAppend cmCommentAppend:cmCommentAppends){
                idList.add(cmCommentAppend.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param cmCommentAppend 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertCmCommentAppend(CmCommentAppend cmCommentAppend) {
        if (Objects.isNull(cmCommentAppend)) {
            return -1;
        }
        try {
        
            //cmCommentAppend.setSpell(PinYin.getPinYin(cmCommentAppend.getName()));
            return cmCommentAppendMapper.insert(cmCommentAppend);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param cmCommentAppend 实体类
     * @return 结果
     */
    @Override
    public int updateCmCommentAppendById(CmCommentAppend cmCommentAppend) {
        if (Objects.isNull(cmCommentAppend)) {
            return -1;
        }
        try {
            //cmCommentAppend.setSpell(PinYin.getPinYin(cmCommentAppend.getName()));
            return cmCommentAppendMapper.updateById(cmCommentAppend);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteCmCommentAppendByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return cmCommentAppendMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<CmCommentAppend> getCmCommentAppendList() {
        try {
            LambdaQueryWrapper<CmCommentAppend> queryWrapper = new LambdaQueryWrapper<>(new CmCommentAppend());
            queryWrapper.isNotNull(CmCommentAppend::getId);
            return cmCommentAppendMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
